package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.*;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.dto.ProductForIdDto;
import com.cskaoyan.shopping.service.INcpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Mr.Tang
 * @since 2022/09/20 22:17
 */
@Service
public class INcpServiceImpl implements INcpService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    NavigationForCommodityConverter navigationForCommodityConverter;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ProductCateConverter productCateConverter;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ProductForCommodityConverter productForCommodityConverter;



    /**
     * 导航栏展示
     * @return
     */
    @Override
    public List<PanelContentDto> navigationForCommodity() {
        // 查出数据
        Example example = new Example(PanelContent.class);
        example.createCriteria().andEqualTo("panelId","0");
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
        // 属性映射 panelContentDtos1是自己建的，也能用
//        List<PanelContentDto> panelContentDtos1 = navigationForCommodityConverter.panelContentConverter(panelContents);

        List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);

        return panelContentDtos;
    }


    /**
     * 列举所有商品种类
     * @return
     */
    @Override
    public List<ProductCateDto> categoriesForCommodity() {
      // 查出数据
        List<ItemCat> itemCats = itemCatMapper.selectAll();
        List<ProductCateDto> productCateDtoList = productCateConverter.items2Dto(itemCats);
        return productCateDtoList;
    }




//    @Override
//    public ProductForIdDto productForCommodity(Long id) {
////        Example example = new Example(Item.class);
////        Example.Criteria criteria = example.createCriteria();
////        criteria.andEqualTo("id","id")
//
//        Item item = itemMapper.selectByPrimaryKey(id);
//        if(item == null) {
//            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(),
//            ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
//        }
//        // 属性映射
//        ProductForIdDto productForIdDto = productForCommodityConverter.productForIdDto(item,null);
//
//
//
//        return productForIdDto;
//    }




    /**
     * 查看商品详情
     * @param id
     * @return
     */

    @Override
    public ProductDetailDto productForCommoditys(Long id) {

        Item item = itemMapper.selectByPrimaryKey(id);
        if(item == null) {
            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(),
            ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
        }

        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, null);
        return productDetailDto;
    }
}
